package com.larissa.android.quiz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.larissa.android.quiz.databinding.ActivityWelcomeBinding;
import com.larissa.android.quiz.question.QuestionRepository;

import java.util.Objects;

public class WelcomeActivity extends AppCompatActivity {

    ActivityWelcomeBinding binding;
    public final static int GEOGRAPHY = 1;
    public final static int MATHEMATICS = 2;

    private final MenuProvider menu = new WelcomeMenuProvider(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).setTitle("Welcome");
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        addMenuProvider(menu);
        //intent里传入1表示GEOGRAPHY
        binding.geography.setOnClickListener(view -> {
            if(QuestionRepository.getInstance().getGeoQuestions().isEmpty())
            {
                Toast.makeText(this, "geo questions are empty", Toast.LENGTH_LONG).show();
                return;
            }
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("flag", GEOGRAPHY);
            startActivity(intent);
        });
        //intent里传入2表示MATHEMATICS
        binding.mathematics.setOnClickListener(view -> {
            if(QuestionRepository.getInstance().getMathQuestions().isEmpty())
            {
                Toast.makeText(this, "math questions are empty", Toast.LENGTH_LONG).show();
                return;
            }
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("flag", MATHEMATICS);
            startActivity(intent);
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(isFinishing())
            QuestionRepository.getInstance().saveQuestionsToDB();
    }
}

class WelcomeMenuProvider implements MenuProvider
{
    WelcomeActivity activity;

    public WelcomeMenuProvider(WelcomeActivity activity)
    {
        this.activity = activity;
    }

    @Override
    public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.menu_welcome, menu);
    }

    @Override
    public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
        int id = menuItem.getItemId();
        if(id == R.id.menu_all)
        {
            Intent intent = new Intent(activity, QuestionActivity.class) ;
            activity.startActivity(intent);
            return true;
        }
        if(id == R.id.menu_create)
        {
            Intent intent = new Intent(activity, CreateQuestionActivity.class) ;
            activity.startActivity(intent);
            return true;
        }
        return false;
    }
}